// BalanceQueryJFrame.java
package JFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import Class.User;

import static JFrame.LoginJFrame.user;

public class BalanceQueryJFrame extends JFrame {

    private JLabel balanceLabel;

    public BalanceQueryJFrame() {
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(2);
        this.setLayout(new FlowLayout());
        this.setVisible(true);
        // 设置窗口标题
        setTitle("查询余额");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(300, 200);
        setLocationRelativeTo(null);

        // 创建面板和组件
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(3, 1, 10, 10));

        balanceLabel = new JLabel("您的余额将在此显示", SwingConstants.CENTER);

        JButton queryButton = new JButton("查询余额");
        queryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    // 执行查询余额的操作
                    double balance = querySumBalance(user);
                    balanceLabel.setText(String.format("您的余额: %.2f", balance));
                } catch (SQLException ex) {
                    balanceLabel.setText("查询余额失败: " + ex.getMessage());
                }
            }
        });

        panel.add(queryButton);
        panel.add(balanceLabel);

        add(panel);
    }

    public double queryDemandBalance(User user) throws SQLException {
        String sql = "SELECT demandMoney FROM user where cardNumber = ?";

        try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "20222746");
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, user.getCardNumber());
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    double demandMoney = rs.getDouble("demandMoney");
                    return demandMoney;
                } else {
                    throw new SQLException("没有找到用户记录");
                }
            }
        }
    }

    public double queryTimeBalance(User user) throws SQLException {
        String sql = "SELECT timeMoney FROM user where cardNumber = ?";

        try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "20222746"); PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, user.getCardNumber());
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    double timeMoney = rs.getDouble("timeMoney");
                    return timeMoney;
                } else {
                    throw new SQLException("没有找到用户记录");
                }
            }
        }
    }
    public double querySumBalance(User user) throws SQLException {
        double balance = 0;
        balance += queryDemandBalance(user);
        balance += queryTimeBalance(user);
        return balance;
    }
}
